package com.sunrise.xdoc.entity.user;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.security.core.GrantedAuthority;

import com.sunrise.xdoc.entity.AbstractCodeEntity;

@Entity
@Table(name = "security_role")
public class Role extends AbstractCodeEntity implements GrantedAuthority {
	// default roles
	public static final String ADMIN = "ADMIN";
	public static final String STOREKEEPER = "STOREKEEPER";
	public static final String ACCOUNTANT = "ACCOUNTANT";
	public static final String CASHIER = "CASHIER";
	public static final String SALER = "SALER";

	private static final long serialVersionUID = 1L;
	private String name;
	private Set<User> users = new HashSet<User>();

	public Role() {
	}

	public Role(String name) {
		this.name = name;
	}

	@Column(name = "name", unique = true)
	@Length(max = 250)
	@NotBlank
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Transient
	@Override
	public String getAuthority() {
		return name;
	}

	@ManyToMany(mappedBy = "roles", fetch = FetchType.EAGER)
	public Set<User> getUsers() {
		return users;
	}

	public void setUsers(Set<User> users) {
		this.users = users;
	}

	@Override
	public boolean equals(Object obj) {
		return obj instanceof Role && ((Role) obj).getName().equals(this.name) && super.equals(obj);
	}

	@Transient
	@Override
	public String toString() {
		return name;
	}
}
